package org.xbdframework.sample.sso.authserver.filter.customLogin;


import org.springframework.security.web.authentication.WebAuthenticationDetails;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Mr.Liu
 * @description: TODO
 * @date: 2023/8/8 9:41
 * @version: 1.0.0
 * @copyright: 中道新职坊
 */

public class CustomWebAuthenticationDetails extends WebAuthenticationDetails {

    private final String client;
    private final Long tenantId;

    public String getClient() {
        return client;
    }

    public Long getTenantId() {
        return tenantId;
    }

    public CustomWebAuthenticationDetails(HttpServletRequest request, String client, Long tenantId) {
        super(request);
        this.client = client;
        this.tenantId = tenantId;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof CustomWebAuthenticationDetails) {
            CustomWebAuthenticationDetails details = (CustomWebAuthenticationDetails) obj;
            if (null != client && null == details.getClient()) {
                return false;
            }
            if (null == client && null != details.getClient()) {
                return false;
            }
            if (null != client && !client.equals(details.getClient())) {
                return false;
            }

            if (null != tenantId && null == details.getTenantId()) {
                return false;
            }
            if (null == tenantId && null != details.getTenantId()) {
                return false;
            }
            if (null != tenantId && tenantId.equals(details.getTenantId())) {
                return false;
            }
        } else {
            return false;
        }
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        int code = super.hashCode();
        if (null != client) {
            code = code * (this.client.hashCode() % 7);
        }
        if (null != tenantId) {
            code = code * (this.tenantId.hashCode() % 7);
        }
        return code;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(super.toString()).append(": ");
        sb.append("client: ").append(this.getClient()).append("; ");
        sb.append("tenantId: ").append(this.getTenantId());

        return sb.toString();
    }
}
